package com.google.code.greenplate;

import java.util.List;

import mobi.greenplate.client.connect.ConnectionContext;
import mobi.greenplate.client.domain.Address;
import mobi.greenplate.client.service.AddressService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ShareRideOfferDetails extends Activity {
	private Button mMyLocationButton;
	private Button mViewRouteMapButton;
	private String mDriver = "mark";
	private String mDepartTime;
	private String mFromCity;
	private String mToCity;
	private TextView mTvDriver;
	private TextView mTvDepartTime;
	private TextView mTvFromCity;
	private TextView mTvToCity;
	
	private class MyLocationButtonListener implements OnClickListener {
		public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(ShareRideOfferDetails.this, MyLocationSetup.class);
            startActivity(intent);
		}
	}
	
	private class ViewRouteMapButtonListener implements OnClickListener {
		public void onClick(View v) {
            Intent intent = new Intent(ShareRideOfferDetails.this, ShowMapFromTo.class);
            setIntentExtras(intent);
            startActivity(intent);
		}
	}	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_ride_offer_details);
        setWidget();
        addListeners();
        getIntentExtras();       
        final List<Address> addressList = getAddressService().findAll(0, 10);
        final Address address = addressList.get(0);
        /*
        Address newAddress = new Address();
        newAddress.setCity(textView.getValue());
        getAddressService().save(address);
        */
       
        mTvDriver.setText(address.getStreet());
        mTvDepartTime.setText(address.getCity());
        mTvFromCity.setText(address.getState());
        mTvToCity.setText(address.getZip());
        
        /*
        mTvDriver.setText(mDriver);
        mTvDepartTime.setText(mDepartTime);
        mTvFromCity.setText(mFromCity);
        mTvToCity.setText(mToCity);
        */
    }
    
    public static ConnectionContext localhostConnectionContext(){
        final ConnectionContext cc = new ConnectionContext("192.168.1.100", 8080, "testuser", "testuser");
        return cc;
    }
    
    private AddressService getAddressService(){
        return new AddressService(localhostConnectionContext());
    }
    
    private void setWidget() {
    	mMyLocationButton = (Button) findViewById(R.id.myLocationButton);
    	mViewRouteMapButton = (Button) findViewById(R.id.viewRouteMapButton);
    	mTvDriver = (TextView) findViewById(R.id.driver);
    	mTvDepartTime = (TextView) findViewById(R.id.timeLeave);
    	mTvFromCity = (TextView) findViewById(R.id.myLocation);    	
    	mTvToCity = (TextView) findViewById(R.id.destination);    	
    }
    
    private void addListeners() {
    	mMyLocationButton.setOnClickListener(new MyLocationButtonListener()); 
    	mViewRouteMapButton.setOnClickListener(new ViewRouteMapButtonListener());
    }
    
    private void getIntentExtras() {
    	Bundle bundle = getIntent().getExtras();  	    	
    	mDriver = bundle != null ? bundle.getString("driver") : "no driver";
    	mDepartTime = bundle != null ? bundle.getString("departTime") : "no depart time";
    	mFromCity = bundle != null ? bundle.getString("fromCity") : "no from city";
    	mToCity = bundle != null ? bundle.getString("toCity") : "no to city";
    }
    
    private void setIntentExtras(Intent intent) {
    	intent.putExtra("fromCity", mFromCity);
    	intent.putExtra("toCity", mToCity);
    }
}